package XynLeen.GraduationProject.Activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youth.xframe.widget.XToast;

import java.util.ArrayList;
import java.util.List;

import XynLeen.GraduationProject.Bean.TypeBean;
import XynLeen.GraduationProject.R;
import XynLeen.GraduationProject.view.EditLineItem;
import XynLeen.GraduationProject.view.TitleArrowLineItem;
import XynLeen.GraduationProject.view.BaseActivity;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;


public class    Activity_Album_Add extends BaseActivity {


    @Bind(R.id.matter)
    EditLineItem matter;
    @Bind(R.id.sel)
    TitleArrowLineItem sel;
    @Bind(R.id.image)
    ImageView image;
    @Bind(R.id.tv_navigation_label)
    TextView mTvNavigation;
    @Bind(R.id.ib_navigation_back)
    ImageButton mIbNavigation_back;
    @Bind(R.id.ib_navigation_add)
    ImageButton mIbNavigation_add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        mTvNavigation.setText("新建相册");
        mIbNavigation_add.setVisibility(View.INVISIBLE);
    }


    @OnClick(R.id.sel)
    public void sel() {
        takePicture();
    }

    public void takePicture() {
        int selectedMode = MultiImageSelectorActivity.MODE_MULTI;

        boolean showCamera = true;
        int maxNum = 1;

        Intent intent = new Intent(this, MultiImageSelectorActivity.class);
        // 是否显示拍摄图片
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, showCamera);
        // 最大可选择图片数量
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, maxNum);
        // 选择模式
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, selectedMode);
        // 默认选择
        startActivityForResult(intent, 9);
    }

    @OnClick(R.id.ib_navigation_back)
    public void back(){
        Intent intent =new Intent(this,Activity_Album.class);
        startActivity(intent);
        finish();
    }

//    @Override
//    public void onBackPressed() {
//        Intent intent =new Intent(this,Activity_Album.class);
//        startActivity(intent);
//        finish();
//    }

    String url;
    List<String> selectList = new ArrayList<>();
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
       if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case 9:
                    selectList = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                    if (null != selectList && selectList.size() > 0) {
                        url = selectList.get(0);
                        Glide.with(this).load(url).into(image);
                    }
                    break;
            }
        }
    }

    @OnClick(R.id.add)
    public void add() {

        if (TextUtils.isEmpty(matter.getContent())) {
            XToast.error("相册名称不能为空");
            return;
        }

        if (TextUtils.isEmpty(url)) {
            XToast.error("请选择封面");
            return;
        }

        TypeBean bean = new TypeBean();
        bean.setName(matter.getContent());
        bean.setUrl(url);
        bean.save();
        Intent intent =new Intent(this,Activity_Album.class);
        startActivity(intent);
        finish();
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_album_add_type;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void initView() {

    }


}
